package com.x.erp.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.x.erp.model.Car;
import com.x.erp.model.ShareTemplate;
import com.x.erp.rpc.ShareTemplateRpcService;
import com.x.erp.util.Constant;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.apache.commons.lang.StringUtils;
import org.beetl.core.Template;
import org.beetl.ext.servlet.ServletGroupTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.List;

/**
 * 分享模板信息
 */
@RestController
@RequestMapping("/shareTemplate")
public class ShareTemplateController extends BaseController {

	@Autowired
	private FastFileStorageClient fdfsClient; // 文件服务

	@Reference
	private ShareTemplateRpcService shareTemplateRpcService; // 分享模板服务

	/**
	 * 增加分享模板信息
	 * @param shareTemplate
	 * @return
	 */
	@RequestMapping(value = "/insertShareTemplate", method = { RequestMethod.GET, RequestMethod.POST })
	public ResultModel insertShareTemplate(ShareTemplate shareTemplate){
		if (StringUtils.isEmpty(shareTemplate.getUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "用户ID不能为空！");
		}
		if (StringUtils.isEmpty(shareTemplate.getShopId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "商铺ID不能为空！");
		}
		if (shareTemplate.getCarIds() ==null || shareTemplate.getCarIds().length == 0) {
			return new ResultModel(ResultCode.RESULT_FAIL, "车辆ID数组不能为空！");
		}
		shareTemplate = shareTemplateRpcService.insertShareTemplate(shareTemplate);
		shareTemplate = shareTemplateRpcService.selectShareTemplate(shareTemplate);
		createHtml(shareTemplate);
		int res = shareTemplateRpcService.updateShareTemplate(shareTemplate);
		if(res == 0){
			return new ResultModel(ResultCode.RESULT_FAIL, "插入失败！");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功！", shareTemplate);
	}

	/**
	 * 删除车辆列表分享模板信息
	 * @param shareTemplate
	 * @return
	 */
	@RequestMapping(value = "/deleteShareTemplate", method = { RequestMethod.GET, RequestMethod.POST })
	public ResultModel deleteShareTemplate(ShareTemplate shareTemplate){
		if (StringUtils.isEmpty(shareTemplate.getUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "用户ID不能为空！");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "删除成功！", shareTemplateRpcService.deleteShareTemplate(shareTemplate));
	}

	/**
	 * 修改车辆列表分享模板信息
	 * @param shareTemplate
	 * @return
	 */
	@RequestMapping(value = "/updateShareTemplate", method = { RequestMethod.GET, RequestMethod.POST })
	public ResultModel updateShareTemplate(ShareTemplate shareTemplate){
		if (StringUtils.isEmpty(shareTemplate.getShareId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "分享模板ID不能为空！");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "修改成功！", shareTemplateRpcService.updateShareTemplate(shareTemplate));
	}

	/**
	 * 查询表分享模板信息
	 * @param shareTemplate
	 * @return
	 */
	@RequestMapping(value = "/selectShareTemplate", method = { RequestMethod.GET, RequestMethod.POST })
	public ResultModel selectShareTemplate(ShareTemplate shareTemplate){
		if (StringUtils.isEmpty(shareTemplate.getShareId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "分享模板ID不能为空！");
		}
		if (StringUtils.isEmpty(shareTemplate.getUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "用户ID不能为空！");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", shareTemplateRpcService.selectShareTemplate(shareTemplate));
	}

	/**
	 * 查询分享模板信息列表
	 * @param shareTemplate
	 * @return
	 */
	@RequestMapping(value = "/selectShareTemplateList", method = { RequestMethod.GET, RequestMethod.POST })
	public ResultModel selectShareTemplateList(ShareTemplate shareTemplate){
		if (StringUtils.isEmpty(shareTemplate.getUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "用户ID不能为空！");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", shareTemplateRpcService.selectShareTemplateList(shareTemplate));
	}

	private void createHtml(ShareTemplate shareTemplate){
		Template template = ServletGroupTemplate.instance().getGroupTemplate().getTemplate("/shareTemplate/carList.html");
		List<Car> cars = shareTemplate.getCars();
		template.binding("cars", cars);
		template.binding("filePath", Constant.FDFS_CONTENT_PATH);
		template.binding("erpContextPath", Constant.ERP_CONTEXT_PATH);
		template.binding("filePath", Constant.FDFS_CONTENT_PATH);
		template.binding("shareTitle", shareTemplate.getShareTitle());
		template.binding("shopName", shareTemplate.getShop().getShopName());
		template.binding("marketId", shareTemplate.getShop().getMarketId());
		template.binding("shopId", shareTemplate.getShop().getShopId());
		template.binding("shopAddress", shareTemplate.getShop().getShopAddress());
		template.binding("shopLogo", "group1/M00/00/54/CkcEDFq6DyGALxewAADn_8Pw9N0168.png");
		template.binding("userMobile", shareTemplate.getUser().getUserMobile());
		template.binding("userName", shareTemplate.getUser().getUserName());
		StringWriter writer = new StringWriter();
		template.renderTo(writer);
		String fileId = null;
		try {
			InputStream is = new ByteArrayInputStream(writer.toString().getBytes("UTF-8"));
			fileId = fdfsClient.uploadFile(is, "html");
		} catch (IOException e) {
			e.printStackTrace();
		}
		logger.info("fileId=" + fileId);
		shareTemplate.setShareFileUrl(fileId);
	}
}
